<?php

/*------------------------------------------------
*
*   Explay Engine Content Management System.
*   Copyright © 2008 Golovdinov Alexander.
*
*-------------------------------------------------
*
*   Official site: wwww.alex-home.spb.ru/explay.html
*   Contact e-mail: golovdinov@gmail.com
*
*   GNU General Public License original source:
*   http://www.gnu.org/licenses/gpl-3.0.html
*
------------------------------------------------*/


if (!defined ('EXPLAY')) exit;


function site_message ($mess) {
	Templater::load ('messages.tpl');
	Templater::set_var ('message', $mess);
	print Templater::display_part ('message');
}

function site_error ($warn) {
	Templater::load ('messages.tpl');
	Templater::set_var ('message', $warn);
	print Templater::display_part ('error');
}

function block ($block_header, $block_name, $body, $type, $align='') {
	global $db;
	
	if ($type =='t') {
		$block = array ();
		$block['header'] = $block_header;
		$block['name'] = $block_name;
		$block['content'] = $body;
		
		Templater::set_var ('block', $block);
		
		($align=='left')
			? CORE::add_block (Templater::display ('block.tpl'), 'left')
			: CORE::add_block (Templater::display ('block.tpl'), 'right');
	}
	else include $_SERVER['DOCUMENT_ROOT'].'/'.$body;
}

function past_table ($header, $body, $return = false) {
	
	$table = array ();
	$table['header'] = $header;
	$table['content'] = $body;
	
	Templater::set_var ('table', $table);

	if ($return)
		return  Templater::display ('table.tpl');
	else
		print Templater::display ('table.tpl');
}


function get_element ($start, $end) { 
	
	$file = Templater::display ('elements.tpl');
	preg_match ("#$start(.+?)$end#is", $file, $element);
	return $element[1];
	
}

//
// Функция добавления формы для комментариев
function past_com_table ($url, $module, $content_id) {
	global $SITE;
	
	if (my_karma() >= '-10') {
		$form = array ();
		$form['url'] = $url;
		$form['module'] = $module;
		$form['content_id'] = $content_id;
		$form['body'] = isset($_POST['body']) ? htmlspecialchars (replace_symbols ($_POST['body'])) : '';
		$form['user_name'] = User::$properties['user_name'];
		$form['user_email'] = User::$properties['user_email'];
		
		Templater::set_var ('form', $form);
		
		if ($SITE['bot_protect'] == 'yes') {
			Templater::set_var ('captcha', '<span id="captcha_image"><img src="/antibot.gif?comment" align="absmiddle" /></span> <input size="5" type="text" name="code" maxlength="5" value="">".
				"<br /><small>Не видно? <a href="/" onclick="reload_captcha(); return false;"><small>Обновить!</small></a></small>');
		}
		else {
			Templater::set_var ('captcha', '');
		}
		return Templater::display ('add_comment.tpl');
	}
	else return 'У Вас слишком маленькая карма, чтобы добавлять комментарии!';
}



function print_auth_form () {
	print Templater::display ('auth_form.tpl');
}

function big_message ($mess, $url) {
	Templater::set_var ('url', $url);
	Templater::set_var ('message', $mess);
	print Templater::display ('message.tpl');
}

function come_back ($name, $url) {
	Templater::set_var ('text', " <a href='$url'>&larr; Вернуться к $name</a>");
	print Templater::display ('simple_table.tpl');
}

function simple_table ($text, $print = true) {
	Templater::set_var ('text', $text);
	if ($print)
		print Templater::display ('simple_table.tpl');
	else
		return Templater::display ('simple_table.tpl');
}

function past_navblock ($array) {

	global $CORE;
	
	$list = '';
	Templater::load ('nav_block/template.tpl');
	
	foreach ($array as $unit) {
		$name = $unit[0];
		$link = $unit[1];
		$var = $unit[2];
		$value = $unit[3];
		
		$not_var = $unit[4];
		$not_value = $unit[5];
		
		if ($not_var == '' && $not_value == '') $not_value = 1;
		
		$a = array ();
		$a['href'] = $link;
		$a['text'] = $name;
		
		Templater::set_var ('link', $a);
		
		if ($value == '_all') {
			if (isset($_GET[$var]) && $_GET[$not_var] != $not_value)
				$list .= Templater::display_part ('active');
			else
				$list .= Templater::display_part ('typical');
		} else {
			if ($_GET[$var] == $value  && $_GET[$not_var] != $not_value)
				$list .= Templater::display_part ('active');
			else
				$list .= Templater::display_part ('typical');
		}
		
	}
	
	Templater::set_var ('list', $list);
	$CORE->second_navigation = Templater::display_part ('list');
	
}


